package com.itheima.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.*;
import com.itheima.service.BlogService;
import com.itheima.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/tag")
public class WebBlogTagController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;

    //1.GET /tag/getArticleByTagUid  (tagUid,currentPage,pageSize)通过TagUid获取文章//1.GET /tag/getArticleByTagUid  (tagUid,currentPage,pageSize)通过TagUid获取文章
     @RequestMapping("/getArticleByTagUid")
     public Map getList(@RequestParam("tagUid") String tagUid,@RequestParam("currentPage") String currentPage,@RequestParam("currentPage") String pageSize) throws Exception {

         PageInfo<Blog> page=  blogService.getArticleByTagUid(tagUid,currentPage,pageSize);

         List<Blog> blogList = page.getList();
         long total = page.getTotal();

         Map<String, Object> map = new HashMap<>();

         //封装查询到的数据
         List<Map<String,Object>> list=new ArrayList<>();

         for (Blog blog : blogList) {

             Map<String, Object> map1 = new HashMap<>();

             map1.put("title",blog.getTitle());
             map1.put("summary",blog.getSummary());
             map1.put("tagUid",blog.getTagUid());
             map1.put("blogSortUid",blog.getBlogSortUid());
             map1.put("clickCount",blog.getClickCount());
             map1.put("collectCount",blog.getCollectCount());
             map1.put("fileUid",blog.getFileUid());
             map1.put("adminUid",blog.getAdminUid());

             map1.put("isPublish",blog.getIsPublish());
             map1.put("isOriginal",blog.getIsOriginal());
             map1.put("author",blog.getAuthor());
             map1.put("articlesPart",blog.getArticlesPart());

             map1.put("level",blog.getLevel());
             map1.put("sort",blog.getSort());
             map1.put("startComment",blog.getStartComment());

            // map1.put("content",blog.getContent());
             List<Tag> tagList2 = blog.getTagList();
             List<Map<String,Object>> list3=new ArrayList<>();
             for (Tag tag : tagList2) {
                 Map<String, Object> map3 = new HashMap<>();
                 // {"content":"测试标签","clickCount":18,"sort":18,"uid":"5c4c541e600ff422ccb371ee788f59d6","status":1,"createTime":"2020-08-05 16:51:11","updateTime":"2020-08-05 16:51:11"}]
                 map3.put("content",tag.getContent());
                 map3.put("clickCount",tag.getSort());
                 map3.put("sort",tag.getContent());
                 map3.put("uid",tag.getUid());
                 map3.put("status",tag.getStatus());
                 map3.put("createTime", DateUtils.parseDate2String(tag.getCreateTime()));
                 map3.put("updateTime",DateUtils.parseDate2String(tag.getUpdateTime()));
                 list3.add(map3);
             }

             map1.put("tagList",list3);

             String pic_url=null;
             List<String> photoList = blog.getPhotoList();
             for (String s : photoList) {
                 pic_url=s;
             }
             map1.put(" photoList", photoList);

             BlogSort blogSort = blog.getBlogSort();
             Map<String, Object> map4 = new HashMap<>();
             //{"sortName":"测试3","content":"测试3","clickCount":2,"sort":10,"uid":"718c76d4a84e408da23e04949a75e7e8","status":1,"createTime":"2020-03-31 20:08:00","updateTime":"2020-08-05 16:39:56"}
             map4.put("sortName",blogSort.getSortName());
             map4.put("content",blogSort.getContent());
             map4.put("clickCount",blogSort.getClickCount());
             map4.put("sort",blogSort.getSort());
             map4.put("uid",blogSort.getUid());
             map4.put("status",blogSort.getStatus());
             map4.put("createTime",DateUtils.parseDate2String(blogSort.getCreateTime()));
             map4.put("updateTime",DateUtils.parseDate2String(blogSort.getUpdateTime()));
             map1.put("blogSort",map4);

             map1.put("blogSortName",blogSort.getSortName());
             map1.put("photoUrl",pic_url);
             //"uid":"07d5a3bfdbb1267904c6bcf2837761cd","status":1,"createTime":"2020-04-24 08:47:54","updateTime":"2020-08-05 16:32:21"
             map1.put("uid",blog.getUid());
             map1.put("status",blog.getStatus());
             map1.put("createTime",DateUtils.parseDate2String(blog.getCreateTime()));
             map1.put("updateTime",DateUtils.parseDate2String(blog.getUpdateTime()));
             list.add(map1);

         }

         map.put("records", list );

         map.put("total", total);
         map.put("size", pageSize);
         map.put("current", currentPage);

         ArrayList<Object> list2 = new ArrayList<>();
         //"code":"success","data":{"records":[{"title":"hushisheng","summary":"didididi","content":"嗨起来","tagUid":"5c4c541e600ff422ccb371ee788f59d6","blogSortUid":"718c76d4a84e408da23e04949a75e7e8","clickCount":1,"collectCount":0,"isPublish":"1","isOriginal":"1","level":0,"sort":0,"startComment":"1","tagList":[{"content":"测试标签","clickCount":18,"sort":18,"uid":"5c4c541e600ff422ccb371ee788f59d6","status":1,"createTime":"2020-08-05 16:51:11","updateTime":"2020-08-05 16:51:11"}],"blogSort":{"sortName":"测试3","content":"测试3","clickCount":2,"sort":10,"uid":"718c76d4a84e408da23e04949a75e7e8","status":1,"createTime":"2020-03-31 20:08:00","updateTime":"2020-08-05 16:39:56"},"uid":"1281207210759819264","status":1,"createTime":"2020-07-09 20:42:57","updateTime":"2020-07-15 14:51:42"}
         map.put("orders", list2);
         map.put("optimizeCountSql", true);
         map.put("isSearchCount", true);
         Map<String, Object> map2 = new HashMap<>();
         //返回结果
         map2.put("code", "success");
         map2.put("data", map);
         return map2;
     }

    //2.GET /tag/getTagList 获取标签的信息
   // {"code":"success","data":[{"content":"测试标签2","clickCount":0,"sort":4,"uid":"6b0ba63beabccc91c4f8fb938984f8a3","status":1,"createTime":"2020-01-30 22:07:10","updateTime":"2020-06-05 09:38:57"},{"content":"string","clickCount":100,"sort":0,"uid":"05d917a5d3ea5af853ee8d4523e5e100","status":1,"createTime":"2020-08-08 10:48:29","updateTime":"2020-08-08 10:48:29"},{"content":"string3","clickCount":1,"sort":0,"uid":"b24e9368958858886fdc564b0a8f3408","status":1,"createTime":"2020-08-08 10:48:29","updateTime":"2020-08-10 09:48:15"}]}
    @GetMapping("/getTagList")
    public Result getTagList(){

        List<Tag> list;
        try {
            list = tagService.getTagList();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"获取失败");
        }
        return new Result(StatusCode.OK,list);
    }

}
